import javax.swing.JOptionPane;


public class TTTLogic {
	
	private TicTacToePanel tttp;
	private DisplayPanel dp;
	
	public TTTLogic(TicTacToePanel tttp, DisplayPanel dp) {
		this.tttp = tttp;
		this.dp = dp;
		
	}
	public boolean checkWin() {
		
		return (checkVertical() || checkHorizontal() || checkLeftDiagonal() || checkRightDiagonal());
		
	}

	public boolean checkVertical() {
		int count = 0;
		for (int j=0; j<TicTacToePanel.COLS; j++) {
			for(int i=0; i<TicTacToePanel.ROWS-1; i++) {
				if ((tttp.getArray()[i][j].getText().equals("X")||tttp.getArray()[i][j].getText().equals("O")) && 
					 tttp.getArray()[i][j].getText().equals(tttp.getArray()[i+1][j].getText())) {
					count++;
				}
			}
			if (count==TicTacToePanel.ROWS-1) {
				return true;
			} else {
				count = 0;
			}
		}
		return false;
	}
	
	public boolean checkHorizontal() {
		int count = 0;
		for (int i=0; i<TicTacToePanel.ROWS; i++) {
			for(int j=0; j<TicTacToePanel.COLS-1; j++) {
				if ((tttp.getArray()[i][j].getText().equals("X")||tttp.getArray()[i][j].getText().equals("O")) && 
					 tttp.getArray()[i][j].getText().equals(tttp.getArray()[i][j+1].getText())) {
					count++;
				}
			}
			if (count==TicTacToePanel.COLS-1) {
				return true;
			} else {
				count = 0;
			}
		}
		return false;
	}
	
	public boolean checkLeftDiagonal() {
		int count = 0;
		for (int i=0; i<TicTacToePanel.ROWS-1; i++) {
			if ((tttp.getArray()[0][0].getText().equals("X")||tttp.getArray()[0][0].getText().equals("O")) && 
				 tttp.getArray()[0][0].getText().equals(tttp.getArray()[i+1][i+1].getText())) {
				 count++;
			}
		}	
		return (count==TicTacToePanel.COLS-1);
	}
	
	public boolean checkRightDiagonal() {
		int count = 0;
		for (int i=0; i<TicTacToePanel.ROWS-1; i++) {
			if ((tttp.getArray()[0][TicTacToePanel.ROWS-1].getText().equals("X")||
				 tttp.getArray()[0][TicTacToePanel.ROWS-1].getText().equals("O")) && 
				 tttp.getArray()[0][TicTacToePanel.ROWS-1].getText().equals(tttp.getArray()[i+1][TicTacToePanel.COLS-2-i].getText())) {
				 count++;
			}
		}	
		return (count==TicTacToePanel.COLS-1);

	}
	
	
	public void checkTie() {
		int count = 0;
		if (!checkWin()) {
			for (int i=0; i<TicTacToePanel.ROWS; i++) {
				for (int j=0; j<TicTacToePanel.COLS; j++) {
					if (!tttp.getArray()[i][j].isEnabled()) {
						count++;
					}
				}
			}
			if (count == TicTacToePanel.ROWS*TicTacToePanel.COLS) {
				dp.getLabel().setText("Tie!");
				int ans = JOptionPane.showConfirmDialog(null, "Do you want to play again?", "Game Over!", JOptionPane.YES_NO_OPTION);
				if (ans == 0) {
					reset();
				} else {
					System.exit(0);
				}
			}
		}
	}
	
	public void disable() {
		for (int i=0; i<TicTacToePanel.ROWS; i++) {
			for (int j=0; j<TicTacToePanel.COLS; j++) {
				tttp.getArray()[i][j].setEnabled(false);
			}
		}
	}
	
	public void reset() {
		for (int i=0; i<TicTacToePanel.ROWS; i++) {
			for (int j=0; j<TicTacToePanel.COLS; j++) {
				tttp.getArray()[i][j].setEnabled(true);
				tttp.getArray()[i][j].setText("");
				TicTacToePanel.turn = true;
				dp.getLabel().setText("Player 1's Turn!");
			}
		}
	}

} //end TTTLogic
